package com.example.spring.mono.controller;

import com.example.spring.mono.entity.EsBlog;
import com.example.spring.mono.repository.EsBlogReactiveRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ReactiveElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;

@RestController
@RequestMapping("/esBlog")
public class EsBlogController {

    @Autowired
    private  EsBlogReactiveRepository reactiveRepository;
   /* public EsBlogController(EsBlogReactiveRepository reactiveRepository) {
        this.reactiveRepository = reactiveRepository;
    }*/

    @PostMapping("/saveBlog")
   // @ApiOperation(value = "保存博客", notes = "保存博客")
    public Mono<EsBlog> save(@RequestBody EsBlog esBlog) {
        return reactiveRepository.save(esBlog);
    }

    @PostMapping("/saveAllBlog")
   // @ApiOperation(value = "批量保存博客", notes = "批量保存博客")
    public Flux<EsBlog> saveAll(@RequestBody List<EsBlog> list) {
        return reactiveRepository.saveAll(list);
    }

    /**
     * 异步向es存esblog
     */
    @PostMapping("/saveAllBlog_reactive")
    //@ApiOperation(value = "批量保存博客", notes = "批量保存博客")
    public Flux<EsBlog> saveAll_re(@RequestBody List<EsBlog> list) {
        Flux<EsBlog> flux = Flux.fromIterable(list);
        return reactiveRepository.saveAll(flux);
    }

    @GetMapping("/deleteBlogById")
    //@ApiOperation(value = "通过id删除博客", notes = "通过id删除博客")
    public Mono<Void> deleteBlogById(String id) {
        return reactiveRepository.deleteById(id);
    }


    @PostMapping("/deleteAllBlog")
    //@ApiOperation(value = "批量删除博客", notes = "批量删除博客")
    public Mono<Void> deleteAll(@RequestBody List<EsBlog> list) {
        return reactiveRepository.deleteAll(list);
    }

    @GetMapping("/searchById/{id}")
   // @ApiOperation(value = "通过id查询", notes = "通过id查询")
    public Mono<EsBlog> searchById(@PathVariable String id) {
        return reactiveRepository.findById(id);
    }

    @PostMapping("/searchByIds")
    //@ApiOperation(value = "通过id批量查询", notes = "通过id批量查询")
    public Flux<EsBlog> searchAll(@RequestBody List<String> ids) {
        return reactiveRepository.findAllById(ids);
    }

    // 查询所有,并根据id倒序排序
    @GetMapping("/findAll")
    //@ApiOperation(value = "查询所有", notes = "查询所有")
    public Flux<EsBlog> findAll() {//Sort.by("id").descending()
        return reactiveRepository.findAll();
    }


}
